package cn.com.service.impl;

import cn.com.dao.CustomerDao;
import cn.com.dao.impl.CustomerDaoImpl;
import cn.com.domain.Customer;
import cn.com.domain.PageBean;
import cn.com.domain.Room;
import cn.com.service.CustomerService;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {
    private CustomerDao dao=new CustomerDaoImpl();

    @Override
    public List<Customer> findAll() {
        return dao.findAll();
    }

    @Override
    public void addCustomer(Customer customer) {
        dao.addCustomer(customer);
    }

    @Override
    public void delteCustomer(Customer customer) {
        dao.delteCustomer(customer);
    }

    @Override
    public void delteCustomerbyid(String id) {
         dao.delteCustomerbyid(id);
    }

    @Override
    public void alterCustomerbyid(Customer customer, String id) {
        dao.alterCustomerbyid(customer,id);
    }

    @Override
    public PageBean<Customer> findcustomerByPage(String _currentPage, String _rows) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //创建空的PageBean对象
        PageBean<Customer> pb= new PageBean<Customer>();
        //设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //调用dao查询总记录数
        int totalCount=dao.findcustomerTotalCount();
        pb.setTotalCount(totalCount);
        //调用dao查询List集合
        //计算开始的记录索引
        int start=(currentPage-1)*rows;
        List<Customer> list=dao.findcustomerByPage(start,rows);
        pb.setList(list);
        //计算总页码 比如总共5页 就是总数除以记录数字，能除尽就是totalCount/rows 不然加1
        int totalPage=totalCount % rows  == 0 ? totalCount/rows : (totalCount/rows)+1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public Customer findcustomerByidentity_numandphone(String identity_num,String phone) {
        return dao.findcustomerByidentity_numandphone(identity_num,phone);
    }

}
